package com.bjpowernode.springboot.web;

import com.bjpowernode.springboot.domain.UploadResponse;
import com.bjpowernode.springboot.util.DownloadUtil;
import com.bjpowernode.springboot.util.UploadUtil;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/file")
@Api(value = "文件操作",tags = {"文件操作"})
public class FileController{

    @Value("${upload-path}")
    private String uploadPath;
    @Value("${download-path}")
    private String downloadPath;

    @Value("#{'${allow-download-file-extensions}'.split(',')}")
    private List<String> allowFileExtensions;
    /**
     * 上传单个文件
     *
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传单个文件")
    public UploadResponse upload(@ApiParam("文件") @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws Exception {
        // 上传文件，返回保存的文件名称
        String saveFileName = UploadUtil.upload(uploadPath, multipartFile, (originalFilename, md5) -> {
            // 文件后缀
            String fileExtension = FilenameUtils.getExtension(originalFilename);
            return md5;
        });
        return new UploadResponse("", saveFileName);
    }

    /**
     * 上传多个文件
     *
     * @return
     */
    @PostMapping("/uploadList")
    @ApiOperation(value = "上传多个文件")
    public List<UploadResponse> uploadList(@ApiParam("文件") @RequestParam("files") List<MultipartFile> multipartFiles, HttpServletRequest request) throws Exception {
        List<UploadResponse> uploadResponseList = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            // 上传文件，返回保存的文件名称
            String saveFileName = UploadUtil.upload(uploadPath, multipartFile, (originalFilename, md5) -> {
                // 文件后缀
                String fileExtension = FilenameUtils.getExtension(originalFilename);
                return md5;
            });
            uploadResponseList.add(new UploadResponse("", saveFileName));
        }

        return uploadResponseList;
    }

    /**
     * 下载文件
     */
    @GetMapping("/{downloadFileName}")
    @ApiOperation(value = "下载文件", notes = "下载文件")
    public void download(@PathVariable(required = true) String downloadFileName, HttpServletResponse response) throws Exception {
        // 下载目录，既是上传目录
        String downloadDir = uploadPath;
        // 允许下载的文件后缀
        // List<String> allowFileExtensions = springBootPlusProperties.getAllowDownloadFileExtensions();

        // 文件下载，使用默认下载处理器
        // 文件下载，使用自定义下载处理器
        DownloadUtil.download(downloadDir, downloadFileName, allowFileExtensions, response,
                (dir, fileName, file, fileExtension, contentType, length) -> {
                    // 下载自定义处理，返回true：执行下载，false：取消下载
                    return true;
                });
    }
}